//excel文件下载
const downloadFile = function downloadFile(fileName: any, content: any) {
	const blob = base64ToBlob(content); // new Blob([content]);
	// 创建用于下载文件的a标签
	const link = document.createElement('a');
	// 设置下载内容
	link.href = window.URL.createObjectURL(blob);
	// 设置下载文件的名字
	link.download = fileName;
	// 放到页面上
	document.body.appendChild(link);
	// 点击下载文件
	link.click();
	// 从页面移除掉
	document.body.removeChild(link);
	// 释放 URL.createObjectURL() 创建的 URL 对象
	window.URL.revokeObjectURL(link.href);

	//废弃方法
	// const evt = document.createEvent('MouseEvents');
	// evt.initEvent('click', false, false);
	// link.dispatchEvent(evt);
	// document.body.removeChild(link);
};
function base64ToBlob(code: any) {
	const parts = code.split(';base64,');
	const contentType = parts[0].split(':')[1];
	const raw = window.atob(parts[1]);
	const rawLength = raw.length;
	const uInt8Array = new Uint8Array(rawLength);
	for (let i = 0; i < rawLength; ++i) {
		uInt8Array[i] = raw.charCodeAt(i);
	}
	return new Blob([uInt8Array], { type: contentType });
}

//根据url下载附件
const downUrl = (url: string, fileName = '附件模版') => {
	//根据url,下载文件
	const link = document.createElement('a');
	link.href = url;
	link.download = fileName + '.xlsx';
	link.style.display = 'none';
	document.body.appendChild(link);
	link.click();
	document.body.removeChild(link);
};

export { downloadFile, downUrl };
